/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.sys.web;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.utils.excel.ExportExcel;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.modules.sys.entity.Log;
import com.lczyfz.istep.modules.sys.service.LogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 日志Controller
 *
 * @author ThinkGem
 * @version 2013-6-2
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/log")
public class LogController extends BaseController {

    @Autowired
    private LogService logService;

    @RequiresPermissions("sys:log:view")
    @RequestMapping(value = {"list", ""})
    public String list(Log log, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<Log> page = logService.findPage(new Page<Log>(request, response), log);
        model.addAttribute("page", page);
//        logger.debug("=dingh==日志导出==" + page.getList().size());
//        logger.debug("=dingh==exception==" + log.getException());

        return "modules/sys/logList";
    }
    @RequestMapping(value = "export")
    public String export(Model model,RedirectAttributes redirectAttributes, HttpServletResponse response, HttpServletRequest request) {

//        String title=request.getParameter("title");
//        String exception=request.getParameter("exception");
//        String createById=request.getParameter("createById");
//        String requestUri=request.getParameter("requestUri");
//        String beginDate=request.getParameter("beginDate");
//        String endDate=request.getParameter("endDate");
//        Log log=new Log();
//        if(title!=null&&!title.equals(""))
//            log.setTitle(title);
//        if(exception!=null&&!exception.equals(""))
//            log.setException(exception);
//        if(createById!=null&&!createById.equals(""))
//        {
//            User createBy=new User();
//            createBy.setId(createById);
//            log.setCreateBy(createBy);
//
//        }
//        if(requestUri!=null&&!requestUri.equals(""))
//            log.setRequestUri(requestUri);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        try {
//            if(beginDate!=null&&!beginDate.equals(""))
//            {
//                Date date1 = sdf.parse(beginDate);
//                log.setBeginDate(date1);
//                logger.debug("=dingh==date1==" + date1);
//
//            }
//            if(endDate!=null&&!endDate.equals(""))
//            {
//                Date date2 = sdf.parse(endDate);
//                log.setEndDate(date2);
//                logger.debug("=dingh==date2==" + date2);
//
//            }
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }

        try {
            String fileName = "日志导出数据.xlsx";
//            if (log.getBeginDate() == null) {
//                log.setBeginDate(DateUtils.setDays(DateUtils.parseDate(DateUtils.getDate()), 1));
//            }
//            if (log.getEndDate() == null) {
//                log.setEndDate(DateUtils.addMonths(log.getBeginDate(), 1));
//            }

            List<Log> list = logService.findAllList();
            logger.debug("=dingh==日志导出==" + list.size());
            new ExportExcel("日志", Log.class, 2).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导出失败！失败信息：" + e.getMessage());
        }

        return "redirect:"+ Global.getAdminPath()+"/sys/log/?repage";

    }
}
